Drills and Challenges on
|
( solutions - for your reference ) |
■ | You have a 16-bit (unsigned or 2C) whole number (call it given) and would like to obtain a 16-bit output number (call it desired) that can be used to print a 1 if the bits at the 16 and 256 place value positions of given are both 1 and to print a 0 otherwise (which is if at least 1 of the bits of given at the 16 and 256 place value positions is 0). (1) Give a way that uses bitwise operations and masking to produce desired from given. (2) Adapt your solution for (1) to instead print a 0 if the bits at the 16 and 256 place value positions of given are both 1 and to print a 1 otherwise. |
|
■ | You are given 2 (ASCII) characters that are alphabets and you have to determine (using bitwise operations) if they are the same or different, case insensitively, and print a 0 or a non-0 value, as depicted below: |
Fill each of the dotted boxes with an appropriate operation/mask. |
|
■ | The formula for converting from IP-number to IP-address involves evaluating the expression (num/65536)%256, where num is an unsigned whole number. Noting that 65536 and 256 are powers of 2, show how you can use bitwise operations to evaluate such an expression. |
|
■ | You have a non-negative 2C signed whole number i2C. You bitwise-AND i2C and -i2C (i.e., bitwise-AND i2C and the 2's complement of i2C) to obtain fid. Explain why comparing i2C and fid for equality will give true if i2C is some power of 2 and false otherwise, EXCEPT for 1 special case. |
Here's saying it another way in C++ terms: |
Explain why the statement bool isPowerOfTwo = (i2C & -i2C) == i2C; will, EXCEPT for 1 special case, result in isPowerOfTwo taking on true or false depending on whether i2C is some power of 2 or not, respectively. |
What is the special case? |
|
(more may be added at any time) |